//
//  HomeLoveViewController.m
//  News
//
//  Created by weige on 16/5/30.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "HomeLoveViewController.h"
#import "BasicTabBatItem.h"
#import "PJXBounceAnimation.h"
#import "PJXAnimatedTabBarController.h"
#import "PJXAnimatedTabBarItem.h"
#import "PJXIconView.h"
@interface HomeLoveViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *colletView;
    BOOL isAnimations;
}

@end

@implementation HomeLoveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // self.tabBarItem=[BasicTabBatItem tabBarItemInit:@"v2_home" andSelect:@"v2_home_r" andTitle:@"首页"];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self bulidColletView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(imgClick:) name:@"imgClickNote" object:nil];
}

-(void)imgClick:(NSNotification *)note
{
   // NSLog(@"%@",[note.userInfo objectForKey:@"tag"]);
    NSLog(@"通知了");
}
-(void)bulidColletView
{

    //[colletView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //self.automaticallyAdjustsScrollViewInsets=NO;
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing=8;
    layout.minimumLineSpacing=5;
    
    colletView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
    [colletView registerClass:[HomeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [colletView registerClass:[GoodsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [colletView registerClass:[HotImgCollectionViewCell class] forCellWithReuseIdentifier:@"cellActivi"];
    colletView.delegate=self;
    
    colletView.dataSource=self;// UIColor.colorWithCustom(239, g: 239, b: 239)
    UIColor  *colo=   [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    colletView.backgroundColor=(UIColor *)colo;//[UIColor colorWithRed:239 green:239 blue:239 alpha:1];
    self.view.backgroundColor=colo;
    [self.view addSubview:colletView];
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        HomeCollectionReusableView *te=[[HomeCollectionReusableView alloc]init];
        //NSLog(@"ss===%f",CGRectGetMaxY(te.subviews[1].frame));
        return CGSizeMake(SCREEN_WIDTH,CGRectGetMaxY(te.subviews[1].frame));
        
    }
    else
    {
        return CGSizeZero;
    }
    }
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionReusableView *myview;
    if (indexPath.section==0) {
    //    NSLog(@"%@",kind);
        myview=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        return myview;
    }
    else
    {
        return nil;
    }
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        return [HeadView loadHomeData:@"activit"].count;
    }
    else
    {
        return  [HeadView loadGoodData].count;

    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{    if(indexPath.section==0)
    {
        
        HotImgCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellActivi" forIndexPath:indexPath];
        cell.tag=indexPath.row;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(homeImgCell:)];
        [cell addGestureRecognizer:tap];
        //cell.imgview.image=
        dispatch_async(dispatch_get_main_queue(), ^{
            [cell.imgview sd_setImageWithURL:[NSURL URLWithString:[HeadView loadHomeData:@"activ"][indexPath.row][@"img"]]];
        });
        
        [self starAinitomTransform:cell andOffSetY:80 andduration:1];
        return cell;
        
    }
    else
    {
        /*!
         *  @author weige, 16-06-01 11:06:42
         *
         *  @brief 商品的Cell
         */
        GoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
      //  cell.goodsImage //[UIImage imageNamed:@"photo.jpg"];
        Goods *g= (Goods *)  [HeadView loadGoodData][indexPath.row];
        cell.good=g;
        //[self starAinitomTransform:cell andOffSetY:80 andduration:1];
      //  [cell addButtonClick:cell.goodsImage];
       
      //  [self addProductAnimation:cell.goodsImage];
     
        return cell;
    }
   
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [colletView  reloadData];
}
-(void)clickAddShopCarsss
{

    
    NSLog(@"sdsad");
}
/*!
 *  @author weige, 16-06-01 16:06:01
 *
 *  @brief 点击cell
 *
 *  @param sender 触摸参数
 */
-(void)cellClick:(UITapGestureRecognizer *)sender
{
    //NSLog(@"%@",[sender view]);
}
/*!
 *  @author weige, 16-06-01 16:06:11
 *
 *  @brief 广告点击事件
 *
 *  @param sender 触摸参数
 */
-(void)homeImgCell:(UITapGestureRecognizer *)sender
{
    /*!
     触摸事件
     */
    DetailedViewController *dc=[[DetailedViewController alloc]init];
    [self.navigationController pushViewController:dc animated:YES];
    //[self.navigationController presentViewController:dc animated:YES completion:nil];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return CGSizeMake(SCREEN_WIDTH, 140);
       
    }
    else
    {
        return CGSizeMake((GoodsImageWidth-20)/2-4, 250);

    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section==0) {
        
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
    else
    {
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }
}

/*!
 *  @author weige, 16-06-01 15:06:17
 *
 *  @brief 动画
 */
-(void)starAinitomTransform:(UIView *)cell andOffSetY:(CGFloat) offsetY andduration:(NSTimeInterval ) duration
{
    
    cell.transform=CGAffineTransformMakeTranslation(0, offsetY);
    [UIView animateWithDuration:duration animations:^{
        cell.transform = CGAffineTransformIdentity;
    }];
}
/*!
 *  @author weige, 16-06-01 16:06:50
 *
 *  @brief 点击事件
 */

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"2222");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
